package collection;

import java.util.HashSet;
import java.util.Objects;

/**
 * HashSet练习
 */
public class HashExercise02 {

    public static void main(String[] args) {

        /*
            定义一个Employee类，该类包含：private 成员属性 sal name birthday(MyDate类)
            其中birthday为myDate类型(属性:year month day)
            创建三个Employee放入 HashSet中
            当 name 和 birthday 的值相同时，认为是相同员工，不能添加到 HashSet集合中
         */
        HashSet hashSet = new HashSet();
        hashSet.add(new Employees(3000.0, "lmy", new Mydate(1999, 4, 11)));
        hashSet.add(new Employees(4000.0, "张三", new Mydate(1999, 4, 20)));
        hashSet.add(new Employees(5000.0, "李四", new Mydate(1999, 4, 30)));
        hashSet.add(new Employees(6000.0, "李四", new Mydate(1999, 4, 30)));

        System.out.println("hashSet: " + hashSet);

    }
}

class Employees {

    private double sal;

    private String name;

    private Mydate birthday;

    public Employees(double sal, String name, Mydate birthday) {
        this.sal = sal;
        this.name = name;
        this.birthday = birthday;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Mydate getBirthday() {
        return birthday;
    }

    public void setBirthday(Mydate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employees{" +
                "sal=" + sal +
                ", name='" + name + '\'' +
                ", birthday=" + birthday +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        Employees employees = (Employees) o;
        return Objects.equals(name, employees.name) && Objects.equals(birthday.getYear(), employees.birthday.getYear())
                && Objects.equals(birthday.getMonth(), employees.birthday.getMonth()) && Objects.equals(birthday.getDay(), employees.birthday.getDay());
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, birthday.getYear(), birthday.getMonth(), birthday.getDay());
    }
}

class Mydate {

    private int year;

    private int month;

    private int day;

    public Mydate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "Mydate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}
